package com.xindaoapp.happypet.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.bean.AddressDetailInfo;
import com.xindaoapp.happypet.bean.InteralGoodsdesc;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

public class ChangeQueRenFragment extends BaseFragment {



    private TextView vNiuniu, mTitle, mJifen;
    private ImageView mImg;

    InteralGoodsdesc goods = null;
    private TextView mName, mNumber, mAddress;

    AddressDetailInfo addresss = null;

    String addressID = "";
    private TextView mIntegral;
    private RelativeLayout mRaddress;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_changedescto, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vNiuniu = (TextView) view.findViewById(R.id.niuniubi);
        mName = (TextView) view.findViewById(R.id.name);
        mNumber = (TextView) view.findViewById(R.id.phone_number);
        mAddress = (TextView) view.findViewById(R.id.address);
        mRaddress = (RelativeLayout) view.findViewById(R.id.r_address);

        mImg = (ImageView) view.findViewById(R.id.img);
        mTitle = (TextView) view.findViewById(R.id.title);
        mJifen = (TextView) view.findViewById(R.id.jifen);

        mIntegral = (TextView) view.findViewById(R.id.tv_integral);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);

        mRaddress.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Intent intent = new Intent(mActivity, MainActivity.class);
                // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                // ConsigneeFragment.class.getCanonicalName());
                // mActivity.startActivity(intent);
                Bundle bundle = new Bundle();
                startFragment(new ConsigneeFragment(), bundle);
            }
        });

        mIntegral.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (TextUtils.isEmpty(addressID)) {
                    Toast.makeText(mActivity, "未获取到收件人地址，请设置!!!", 0).show();
                } else {
                    goIntegral();
                }
            }
        });

    }

    // 确认兑换 --先扣牛牛币
    protected void goIntegral() {
        getMoccaApi().deleteIntegral(String.valueOf(goods.goods_id), goods.exchange_integral,
                new IRequest<Pair<Boolean, String>>() {

                    @Override
                    public void request(Pair<Boolean, String> t) {
                        if (null != t) {
                            if (t.first) {
                                // Toast.makeText(getActivity(), "扣钱成功~",
                                // Toast.LENGTH_SHORT).show();
                                addIntegralGoods();
                            } else {
                                Toast.makeText(getActivity(), t.second, Toast.LENGTH_SHORT).show();
                            }
                        } else {

                            try {
                                Toast.makeText(getActivity(), R.string.net_error,
                                        Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    /**
     * 扣钱成功
     */
    protected void addIntegralGoods() {

        String uid = CommonUtil.decode(UserState.getUser().uid);
        getMoccaApi().addIntegralGoods(uid, String.valueOf(goods.ex_id), addressID,
                new IRequest<Pair<Boolean, String>>() {

                    @Override
                    public void request(Pair<Boolean, String> t) {
                        try {
                            if (null != t) {
                                if (t.first) {
                                    Toast.makeText(getActivity(), "兑换商品成功", Toast.LENGTH_SHORT)
                                            .show();
                                    // Toast.makeText(getActivity(), t.second, Toast.LENGTH_SHORT)
                                    // .show();

                                    Intent intent = new Intent(mActivity, MainActivity.class);
                                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                                            NiuniuOrderFragment.class.getCanonicalName());
                                    mActivity.startActivity(intent);
                                    mActivity.finish();
                                    // startFragment(new NiuniuOrderFragment());

                                } else {
                                    Toast.makeText(getActivity(), t.second, Toast.LENGTH_SHORT)
                                            .show();
                                }
                            } else {
                                Toast.makeText(getActivity(), R.string.net_error,
                                        Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        vNiuniu.setText(UserState.getUser().niuniu);// 设置牛牛币
        getMoccaApi().getDefaultAddress(UserState.getUserUid(), new IRequest<AddressDetailInfo>() {

            @Override
            public void request(AddressDetailInfo t) {
                // addresss = t;
                if (null != t) {
                    addressID = t.address_id;
                    mName.setText(t.username);
                    mNumber.setText(t.mobile);
                    mAddress.setText(t.address);
                } else {
                    mName.setText("未设置收货地址");
                    mNumber.setText("");
                    mAddress.setText("");
                }
            }

        });


        if (null != getArguments()) {
            String exid = getArguments().getString("ex_id");
            getMoccaApi().getChangeDesc(exid, new IRequest<InteralGoodsdesc>() {

                @Override
                public void request(InteralGoodsdesc t) {
                    if (t != null) {
                        goods = t;
                        ImageLoader.getInstance().displayImage(goods.goods_thumb, mImg);
                        mTitle.setText(goods.goods_name);
                        mJifen.setText(goods.exchange_integral);
                    }
                }

            });
        }

    }

    @Override
    protected String getTitleName() {
        return "确认订单";
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected String getRightName() {
        return "更改地址";
    }

    @Override
    protected boolean showRightView() {
        return true;
    }

    @Override
    protected void setRightViewListener(View view) {
        super.setRightViewListener(view);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Intent intent = new Intent(mActivity, MainActivity.class);
                // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                // ConsigneeFragment.class.getCanonicalName());
                // mActivity.startActivity(intent);
                startFragment(new ConsigneeFragment());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadData();
    }

    @Override
    protected void onFragmentResult(Bundle bundle) {
        super.onFragmentResult(bundle);
        onLoadData();
    }

}
